package com.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;

public class BackFileUtil {
	/**
	 * 
	 * @param TargetPath
	 * @param className
	 * @param classTail
	 * @param backRootFile
	 */
	public static void backFile(String srcPath,String TargetPath,String className,String classTail,String backRootFile){
		String fileName =Global.WORKSPACEPROPATH+srcPath+TargetPath+"//"+className+classTail;
		String backFileName =backRootFile+srcPath+TargetPath+"//"+className+classTail;
		createFile(backRootFile+srcPath+TargetPath,className+classTail);
		Logger.getLogger(BackFileUtil.class).info("备份文件："+fileName);
		try {
			copyFile(fileName,backFileName);
			Logger.getLogger(BackFileUtil.class).info("备份到文件："+backFileName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void copyFile(String oldAddress, String newAddress) throws Exception {
	        FileInputStream input=new FileInputStream(oldAddress);
	        FileOutputStream output=new FileOutputStream(newAddress);
	        int in=input.read();
	        while(in!=-1){
	            output.write(in);
	            in=input.read();
	        }
	        input.close();
	        output.close();
	}
	
	public static void main(String args[]) throws IOException{
		/*File file = new File("F:\\gitRepository\\PM\\back\\ccc\\aa.txt");
	      if (!file.exists()) {
	          new File(file.getParent()).mkdirs();
	        }*/
		//createFile();
		
		File file = new File("F:\\gitRepository\\PM\\back\\ccc\\aa.txt");
		if(!file.exists()) {
			file.mkdirs();
		}
		
	}
	
	public static void createFile(String path,String fileName){

        File f = new File(path);

        if(!f.exists()){

            f.mkdirs();//创建目录
        }
        File file = new File(path, fileName);

        if(!file.exists()){

            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

}
}
